package com.glory.brepda.activity.lanjian;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class BREtowFCActivity extends BaseActivity {

    @BindView(R.id.tvStepdesc)
    TextView tvStepdesc;
    @BindView(R.id.tvMainqty)
    TextView tvMainqty;
    @BindView(R.id.tvproductmodule)
    FreeUI_ClearEditText tvproductmodule;
    @BindView(R.id.tvMainMatType)
    FreeUI_ClearEditText tvMainMatType;
    @BindView(R.id.tvUserName)
    TextView tvUserName;
    @BindView(R.id.tvDesc)
    TextView tvDesc;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;

    private LotAndPackagingfileEntity entity;
    private String username;
    private String usernameDesc;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bretow_fcactivity;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        username = Constant.CURRENT_USER;
        usernameDesc = Constant.CURRENT_DESC;

        initEdit(etLotID, sa -> {
            //获取lot信息
            FreeUI_EntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>>
                    (new TypeToken<BaseResponseBean<LotAndPackagingfileEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        entity = response.body().getResponse().getBody();
                        inputvalue(entity);
                        showToast("成功", true);
                        etLotID.requestFocus();
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "失败");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("LOT_ID", getText(etLotID));
            map.addProperty("ACTION_TYPE", "QUERY");
            NetUtils.requestPostNet("IF.PDA_QUERY_LOT_PACKS", "Common", getApplication(), map, callBack);
        });
    }

    private void  inputvalue(LotAndPackagingfileEntity entity){

        etLotID.setText(entity.getLOT().getLOTID());
        tvStepdesc.setText(entity.getLOT().getSTEPDESC());
        tvMainqty.setText(entity.getLOT().getMAINQTY());
        tvproductmodule.setText(entity.getLOT().getUDF().getProductModel());
        tvMainMatType.setText(entity.getLOT().getMAINMATTYPE());
        tvUserName.setText(username);
        tvDesc.setText(usernameDesc);
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.tvBaseSubmit:

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showToast("提交成功!", true);
                                    etLotID.setText("");
                                    tvStepdesc.setText("");
                                    tvMainqty.setText("");
                                    tvproductmodule.setText("");
                                    tvMainMatType.setText("");
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"EMPLOYEE_OUTPUT");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("EMPLOYEE_ID", username);
                        map.addProperty("EMPLOYEE_NAME", usernameDesc);
                        map.addProperty("LOT_ID", getText(etLotID));
                        map.addProperty("MAIN_MAT_TYPE", tvMainMatType.getText().toString());
                        map.addProperty("PRODUCT_MODEL", tvproductmodule.getText().toString());
                        map.addProperty("MAINQTY", Integer.parseInt(tvMainqty.getText().toString()));
                        map.addProperty("PRO_TIME", "2023-12-30 01:01:01");
                        NetUtils.requestPostNet("IF.EMPLOYEE_OUTPUT", "Common", getApplication(), map, callBack);
                    }
                });
                break;
        }
    }

    @Override
    protected void initData() {

    }
}